<template>
  <a-modal title="分账配置" :width="800" :visible="visible" :confirmLoading="confirmLoading" :maskClosable="false" @ok="handleSubmit"
           @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="门店">
          <a-select showSearch optionFilterProp="children" :disabled="isDisabled"
                    v-decorator="['storeId', {rules: [{required: true, message: '必输'}]}]">
            <a-select-option v-for="item in storelist" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分账账户"
        >
          <a-select showSearch optionFilterProp="children"
                    v-decorator="[ 'receiverId', {rules: [{required: true, message: '必输'}]} ]">
            <a-select-option v-for="item in recevier" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>
        <!--<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="收银台优惠标记">
          <a-input v-decorator="[ 'goodsTag', {rules: [] }]"/>
        </a-form-item>-->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { saveStoreReceiver, selectAllReceiver } from '@/api/shop/receiver'
  import {selectAllStore} from '@/api/shop/storetest'
  /**
   * 数据
   */
  const data = function() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      id: null,
      visible: false,
      confirmLoading: false,
      isDisabled: false,
      storelist:[],
      storeId:null,
      recevier:[],
      form: this.$form.createForm(this),
    }
  }

  const created = function() {
    let storeId = this.editProps[0]
    this.storeId = storeId
    selectAllStore().then(res => {
      if (res.code === 200) {
        for(let value of res.result){
          if(storeId==value.id){
            this.storelist = res.result
          }else{
            this.storelist = res.result
          }
        }
      }
    })
    selectAllReceiver().then(res => {
      if (res.code === 200) {
        this.recevier=res.result
        console.log(res.result)
      }
    })
  }

  export default {
    props: {
      editProps: {
        type: Array,
        default: null
      }
    },
    data: data,
    created: created,
    /**
     * 方法
     */
    methods: {
      add () {
        this.visible = true
        if(this.editProps.length!==0){
          this.isDisabled = true
          let storeId = this.form.getFieldValue('storeId')
          this.form.setFieldsValue({
            storeId: this.storeId
          })
        }
      },
      edit (record) {
        console.log(record)
        this.visible = true
        this.id = record.id
        this.isDisabled = true
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          const formData = pick(record, [ 'storeId', 'receiverId'])
          setFieldsValue(formData)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            saveStoreReceiver(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.id = null
        this.visible = false
        this.isDisabled = false
        this.confirmLoading = false
        this.form.resetFields()
        this.imageUrl = null
        this.imageUrl2 = null
        this.filename = null
        this.filename2 = null
      },
      filterOption (input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },
      uploadChange(info) {
        if (info.file.status === 'uploading') {
          this.confirmLoading = true;
          return;
        }
        if (info.file.status === 'done') {
          // Get this url from response in real world.
          this.imageUrl = info.file.response.fileUrl;
          this.filename = info.file.response.filename
          this.confirmLoading = false;
        }
      },
      uploadChange2(info) {
        if (info.file.status === 'uploading') {
          this.confirmLoading = true;
          return;
        }
        if (info.file.status === 'done') {
          // Get this url from response in real world.
          this.imageUrl2 = info.file.response.fileUrl;
          this.filename2 = info.file.response.filename
          this.confirmLoading = false;
        }
      },
      beforeUpload(file) {
        return true;
      },
      dropdown(input){
        console.log(input)
        return input;
      }
    }
  }
</script>